package com.pencilisland.pay.mgt.console.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.pencilisland.pay.mgt.console.entity.SysUser;
import com.pencilisland.pay.mgt.console.service.UserService;

@Controller
@RequestMapping(value = "/console/user")
public class UserController {

	@Autowired
	UserService userService;

	@RequestMapping(value = "")
	public String toUserList() {
		return "console/user";
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> userList(@ModelAttribute SysUser cretiria, @RequestParam(value = "start") int start,
			@RequestParam(value = "length") int length) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		Page<SysUser> userPage = userService.userList(cretiria, start, length);

		resultMap.put("recordsFiltered", userPage.getTotalElements());
		resultMap.put("recordsTotal", userPage.getTotalElements());
		resultMap.put("userList", userPage.getContent());

		return resultMap;
	}

	/**
	 * 保存更新
	 * 
	 * @param org
	 * @return
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public SysUser save(@ModelAttribute SysUser user) {
		return userService.save(user);
	}

	/**
	 * 加载数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/load/{id}")
	@ResponseBody
	public Map<String, Object> load(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("data", userService.loadData(id));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

	@RequestMapping(value = "/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			userService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

	/**
	 * 修改密码
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/modifypassword")
	@ResponseBody
	public Map<String, Object> modifyPassword(@RequestParam(value = "userId") Long userId,
			@RequestParam(value = "oldPassword") String oldPassword,
			@RequestParam(value = "newPassword") String newPassword,
			@RequestParam(value = "newPasswordConfirm") String newPasswordConfirm) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			userService.modifyPassword(userId, oldPassword, newPassword, newPasswordConfirm);
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", e.getMessage());
		}
		return resultMap;
	}

	/**
	 * 上传用户头像
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/avatar/upload")
	@ResponseBody
	public Map<String, Object> uploadAvatar(@RequestParam(value = "file", required = true) MultipartFile file) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			userService.uploadAvatar(file);
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", e.getMessage());
		}
		return resultMap;
	}

	/**
	 * 获取用户头像
	 * 
	 * @return
	 */
	@RequestMapping(value = "/avatar")
	public ResponseEntity<byte[]> avatar() {

		byte[] result = userService.avatar();

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_JPEG);
		return new ResponseEntity<byte[]>(result, headers, HttpStatus.OK);
	}

	/**
	 * 重置用户密码
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/password/reset")
	@ResponseBody
	public Map<String, Object> resetPassword(@RequestParam(value = "userId", required = true) Long userId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			userService.resetPassword(userId);
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", e.getMessage());
		}
		return resultMap;
	}

}
